#include "GenericKeys.h"
#include "General Utils.hpp"

map<string, OGL_Color> GenericKeys::IDToColorMap;
map<string, GLFT_Font> GenericKeys::IDToGLFT_Font;

GenericKeys* GenericKeys::Instance()
{
	static GenericKeys instance;
	return &instance;
}

void GenericKeys::Init()
{
	// Colors
	IDToColorMap.insert(std::pair<string, OGL_Color>("green", green));
	IDToColorMap.insert(std::pair<string, OGL_Color>("blue", blue));
	IDToColorMap.insert(std::pair<string, OGL_Color>("red", red));
	IDToColorMap.insert(std::pair<string, OGL_Color>("white", white));
	IDToColorMap.insert(std::pair<string, OGL_Color>("black", black));
	IDToColorMap.insert(std::pair<string, OGL_Color>("gray", gray));
	OGL_Color nothing;
	nothing.r = 1.0f; nothing.g = 1.0f; nothing.b = 1.0f; nothing.a = 0.0f;
	IDToColorMap.insert(std::pair<string, OGL_Color>("none", nothing));

	// Fonts
	int font_size = 16;
	int small_font_size = 12;
	Crystal.open("Fonts\\Crystal.ttf", font_size);
	Bank_Printer.open("Fonts\\Bank_Printer.ttf", font_size);
	Inconsolata.open("Fonts\\Inconsolata.otf", font_size);
	Onuava.open("Fonts\\Onuava.ttf", font_size);
	Self_Destruct.open("Fonts\\Self_Destruct.otf", font_size);
	Onuava_Small.open("Fonts\\Onuava.ttf", small_font_size);




	IDToGLFT_Font.insert(std::pair<string, GLFT_Font>("crystal", Crystal));
	IDToGLFT_Font.insert(std::pair<string, GLFT_Font>("bank_printer", Bank_Printer));
	IDToGLFT_Font.insert(std::pair<string, GLFT_Font>("inconsolata", Inconsolata));
	IDToGLFT_Font.insert(std::pair<string, GLFT_Font>("onuava", Onuava));
	IDToGLFT_Font.insert(std::pair<string, GLFT_Font>("Onuava", Onuava));
	IDToGLFT_Font.insert(std::pair<string, GLFT_Font>("self_destruct", Self_Destruct));

	Onuava_Big.open("Fonts\\Onuava.ttf", font_size * 2);
	IDToGLFT_Font.insert(std::pair<string, GLFT_Font>("onuava big", Onuava_Big));
	IDToGLFT_Font.insert(std::pair<string, GLFT_Font>("onuava_big", Onuava_Big));
	IDToGLFT_Font.insert(std::pair<string, GLFT_Font>("onuava small", Onuava_Small));

	Crystal_Big.open("Fonts\\Crystal.ttf", font_size * 1.5);
	IDToGLFT_Font.insert(std::pair<string, GLFT_Font>("crystal big", Crystal_Big));


	m_PanelIDs.Menu_Screen		= 701;
	m_PanelIDs.Menu_NewGame	= 702;
	m_PanelIDs.Menu_Editor		= 703;
	m_PanelIDs.Menu_Exit		= 704;
	m_PanelIDs.Menu_MapSelector= 705;

	m_PanelIDs.Game_Unit		= 801;

	m_PanelIDs.Editor_Tiles					= 901;
	m_PanelIDs.Editor_Algorithms			= 902;
	m_PanelIDs.Editor_AlgorithmsSNAKEFAST	= 903;
	m_PanelIDs.Editor_AlgorithmsBOX			= 904;
	m_PanelIDs.Editor_AlgorithmsFOCUSPOINT	= 905;

	m_PanelIDs.Editor_Buttons					= 950;
	m_PanelIDs.Editor_buttonID_displaySelection	= 951;
	m_PanelIDs.Editor_buttonID_buildRoom		= 952;
	m_PanelIDs.Editor_buttonID_buildPath		= 953;
	m_PanelIDs.Editor_buttonID_saveToLua		= 954;
	m_PanelIDs.Editor_buttonID_moveRooms		= 955;
	m_PanelIDs.Editor_buttonID_removeRoom		= 956;

	m_PanelIDs.MapSelector_Screen	= 1001;
	m_PanelIDs.MapSelector_Close	= 1002;
	m_PanelIDs.MapSelector_NotRec	= 1003;
}

void GenericKeys::Destroy()
{
	Crystal.release();
	Bank_Printer.release();
	Inconsolata.release();
	Onuava.release();
	Self_Destruct.release();
	Onuava_Big.release();
	Crystal_Big.release();
	Onuava_Small.release();

	// Possible memory errors later on
	IDToGLFT_Font["crystal"].release();
	IDToGLFT_Font["bank_printer"].release();
	IDToGLFT_Font["inconsolata"].release();
	IDToGLFT_Font["onuava"].release();
	IDToGLFT_Font["self_destruct"].release();
	IDToGLFT_Font["onuava big"].release();
	IDToGLFT_Font["onuava_big"].release();
	IDToGLFT_Font["crystal big"].release();
}

OGL_Color GenericKeys::GetColor(const char* id)
{
	string parse = id;

	size_t found;

	found = parse.find(",");

	if(found == string::npos)
		return (IDToColorMap[id]);

	else
	{
		float r, g, b, a;

		/*if(parse.size() != 17 && parse.size() != 12)
		{		
			#ifdef CONSOLE_DEBUG
			cout << "The color passed was " << parse << endl;
			#endif
			#ifdef EXECUTE_DEBUG
			ExecuteLog->Print("ERROR: Bad color passed ~");
			ExecuteLog->Print(parse);
			#endif

			assert("Bad color passed (number format)" && false);
		}*/

		// r
		string getnum = "init";
		getnum.clear();
		getnum.push_back(parse[0]);
		getnum.push_back(parse[1]);
		getnum.push_back(parse[2]);
		//getnum = parse.at(0) + parse.at(1) + parse.at(2);
		r = atof(getnum.c_str());

		// g
		getnum.clear();
		getnum.push_back(parse[5]);
		getnum.push_back(parse[6]);
		getnum.push_back(parse[7]);
		//getnum = parse[5] + parse[6] + parse[7];
		g = atof(getnum.c_str());

		// b
		getnum.clear();
		getnum.push_back(parse[10]);
		getnum.push_back(parse[11]);
		getnum.push_back(parse[12]);
		//getnum = parse[10] + parse[11] + parse[12];
		b = atof(getnum.c_str());

		// a (only if given, otherwise full alpha)
		if(parse.size() == 18)
		{
			getnum.clear();
			getnum.push_back(parse[15]);
			getnum.push_back(parse[16]);
			getnum.push_back(parse[17]);
			//getnum = parse[14] + parse[15] + parse[16];
			a = atof(getnum.c_str());
		}
		else
			a = 1.0f;

		return (GetFixedColor(r, g, b, a));
	}

}